package com.example.wyz.classplus.ui.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.NavigationView;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.widget.Toolbar;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.Spinner;
import android.widget.TextView;

import com.example.wyz.classplus.R;
import com.example.wyz.classplus.base.BaseActivity;
import com.example.wyz.classplus.contract.atv.MainContract;
import com.example.wyz.classplus.contract.fgm.TimeTableContract;
import com.example.wyz.classplus.presenter.MainPresenter;
import com.example.wyz.classplus.ui.adapter.MainPagerAdapter;
import com.example.wyz.classplus.ui.fragment.MainMessageFragment;
import com.example.wyz.classplus.ui.fragment.QAFragment;
import com.example.wyz.classplus.ui.fragment.TimeTableFragment;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * Created by 10153 on 2017/5/21.
 */

public class MainActivity extends BaseActivity<MainContract.Presenter> implements MainContract.View{

    @BindView(R.id.mViewPager) ViewPager mViewPager;
    @BindView(R.id.mTabLayout) TabLayout mTabLayout;
    @BindView(R.id.mDrawerLayout) DrawerLayout mDrawerLayout;
    @BindView(R.id.navigation_view) NavigationView mNavigation;

    private List<Fragment> mFragments = new ArrayList<>();
    private String[] tabs = new String[]{"消息", "课表", "问答"};
    private MainPagerAdapter adapter;
    private ActionBarDrawerToggle mDrawerToggle;

    private LinearLayout currentToolbarLayout;
    private LinearLayout mToolbarQALayout;
    private LinearLayout mToolbarTTLayout;
    private LinearLayout mToolbarMMLayout;
    private Spinner QASpinner;
    private Spinner TTSpinner;
    private TextView test;

    private int currentPagerPage = 1;

    //BaseActivity方法重写&接口实现
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initViewPager();
        initNavigation();
        initToolbarLayout();
    }

    @Override
    protected void onPostCreate(@Nullable Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);
        mDrawerToggle.syncState();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        //getMenuInflater().inflate(R.menu.menu_main_time_table, menu);
        return true;
    }

    @Override
    public boolean onPrepareOptionsMenu(Menu menu) {
        menu.clear();
        switch (currentPagerPage){
            case 0:
                getMenuInflater().inflate(R.menu.menu_main_msg, menu);
                break;
            case 1:
                getMenuInflater().inflate(R.menu.menu_main_time_table, menu);
                break;
            case 2:
                getMenuInflater().inflate(R.menu.menu_main_qa, menu);
                break;
            default:
                break;
        }
        return super.onPrepareOptionsMenu(menu);
    }

    @Override
    protected int getLayout() {
        return R.layout.activity_main;
    }

    @Override
    protected void initPresenter() {
        this.mPresenter = new MainPresenter(this, this);
    }

    //contract.view接口实现

    //私有方法
    private void initViewPager(){
        mFragments.add(new MainMessageFragment());
        mFragments.add(new TimeTableFragment());
        mFragments.add(new QAFragment());


        adapter = new MainPagerAdapter(getSupportFragmentManager(), mFragments, tabs);
        mViewPager.setAdapter(adapter);
        mViewPager.setOffscreenPageLimit(2);
        mViewPager.setCurrentItem(currentPagerPage);
        mTabLayout.setupWithViewPager(mViewPager);

        mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {

            @Override
            public void onPageScrolled(int position, float positionOffset,
                                       int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                currentPagerPage = position;
                invalidateOptionsMenu();
                changeToolbarLayoutFromPage();
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    private void initNavigation(){
        mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, mToolBar,
                                                  R.string.drawer_open, R.string.drawer_close);
        mDrawerLayout.addDrawerListener(mDrawerToggle);
    }

    private void initToolbarLayout(){
        mToolbarQALayout = (LinearLayout) LayoutInflater.from(this).inflate(R.layout.layout_qa_spinner, mToolBar, false);
        mToolbarTTLayout = (LinearLayout) LayoutInflater.from(this).inflate(R.layout.layout_time_table_select, mToolBar, false);
        mToolbarMMLayout = new LinearLayout(this);
        QASpinner = (Spinner)mToolbarQALayout.findViewById(R.id.spinner_qa);
        TTSpinner = (Spinner)mToolbarTTLayout.findViewById(R.id.spinner_tt);
        TTSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                ((TimeTableContract.View)adapter.getItem(1)).setSelectWeek(i+1);
            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {

            }
        });

        currentToolbarLayout = mToolbarTTLayout;
        changeToolbarLayoutFromPage();
    }

    private void changeToolbarLayoutFromPage(){
        mToolBar.removeView(currentToolbarLayout);
        switch (currentPagerPage){
            case 0:
                currentToolbarLayout = mToolbarMMLayout;
                break;
            case 1:
                currentToolbarLayout = mToolbarTTLayout;
                break;
            case 2:
                currentToolbarLayout = mToolbarQALayout;
                break;
            default:
                break;
        }
        Toolbar.LayoutParams params
                = new Toolbar.LayoutParams(Toolbar.LayoutParams.WRAP_CONTENT,
                                           Toolbar.LayoutParams.WRAP_CONTENT,
                                           Gravity.CENTER);
        mToolBar.addView(currentToolbarLayout, params);
    }
}
